<html>
<head><meta charset="utf-8"><title>abstract consts unifying wrong because of mir lowering · project-const-generics · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/index.html">project-const-generics</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/abstract.20consts.20unifying.20wrong.20because.20of.20mir.20lowering.html">abstract consts unifying wrong because of mir lowering</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="242097255"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/abstract%20consts%20unifying%20wrong%20because%20of%20mir%20lowering/near/242097255" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Boxy [she/her] <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/abstract.20consts.20unifying.20wrong.20because.20of.20mir.20lowering.html#242097255">(Jun 09 2021 at 16:55)</a>:</h4>
<p>cc <span class="user-mention" data-user-id="124288">@oli</span> </p>
<p>currently we're using MIR to unify non-concrete abstract consts in the <code>const_evaluatable_checked</code> feature but this can cause some stuff to unify when it probably shouldn't. e.g. <a href="https://github.com/rust-lang/rust/pull/86130#pullrequestreview-679812705">this PR</a> has an issue where the following compiles:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#![feature(const_generics, const_evaluatable_checked)]</span><span class="w"></span>
<span class="k">struct</span> <span class="nc">Foo</span><span class="o">&lt;</span><span class="k">const</span><span class="w"> </span><span class="n">N</span>: <span class="kt">u8</span><span class="o">&gt;</span><span class="p">([</span><span class="kt">u8</span><span class="p">;</span><span class="w"> </span><span class="n">N</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="kt">usize</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="kt">usize</span><span class="p">])</span><span class="w"> </span><span class="k">where</span><span class="w"> </span><span class="n">evaluatable</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">N</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="kt">usize</span><span class="w"> </span><span class="p">};</span><span class="w"></span>
</code></pre></div>
<p>despite having no predicate for <code>N as usize as usize</code> being evaluatable</p>
<p>this seems to be caused by MIR not keeping the second as cast around (no idea when this removal actually happens maybe its before MIR lowering?)</p>
<p>can see the MIR <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=0d8151b3c1c37a477ac241a71d2df305">here</a></p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">const</span><span class="w"> </span><span class="k">fn</span> <span class="nf">foo</span><span class="p">(</span><span class="n">arg</span>: <span class="kt">u8</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">usize</span> <span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">arg</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="kt">usize</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="kt">usize</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<div class="codehilite"><pre><span></span><code>fn foo(_1: u8) -&gt; usize {
    debug arg =&gt; _1;
    let mut _0: usize;
    let mut _2: u8;

    bb0: {
        _2 = _1;
        _0 = move _2 as usize (Misc);
        return;
    }
}
</code></pre></div>
<p>we should probably figure out a way to unify generic consts that isnt as prone to exposing details about how we lower stuff or else const_evaluatable_checked could have some undesirable back compat implications?</p>
<p>going to open an issue somewhere about this but just opening a zulip topic for the moment</p>



<a name="242098169"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/abstract%20consts%20unifying%20wrong%20because%20of%20mir%20lowering/near/242098169" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Boxy [she/her] <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/abstract.20consts.20unifying.20wrong.20because.20of.20mir.20lowering.html#242098169">(Jun 09 2021 at 17:02)</a>:</h4>
<p>I know that lcnr has talked about wanting to use thir for abstract consts that way there's less lowering that takes place</p>



<a name="243360539"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/abstract%20consts%20unifying%20wrong%20because%20of%20mir%20lowering/near/243360539" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/abstract.20consts.20unifying.20wrong.20because.20of.20mir.20lowering.html#243360539">(Jun 21 2021 at 09:55)</a>:</h4>
<p><span aria-label="thumbs up" class="emoji emoji-1f44d" role="img" title="thumbs up">:thumbs_up:</span> seems like we have to use thir or something for this</p>



<a name="243360556"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/260443-project-const-generics/topic/abstract%20consts%20unifying%20wrong%20because%20of%20mir%20lowering/near/243360556" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/260443-project-const-generics/topic/abstract.20consts.20unifying.20wrong.20because.20of.20mir.20lowering.html#243360556">(Jun 21 2021 at 09:55)</a>:</h4>
<p>this seems unfortunate</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>